<template>
	<form-container title="新增" :width="550" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="编码：" name="code">
				<a-input v-model:value="formData.code" placeholder="请输入编码" allow-clear />
			</a-form-item>
			<a-form-item label="名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入名称" allow-clear />
			</a-form-item>
			<a-form-item label="类型：" name="type">
				<a-select v-model:value="formData.type" placeholder="请选择类型">
					<a-select-option value="一般">一般</a-select-option>
					<a-select-option value="测试">测试</a-select-option>
					<a-select-option value="维修">维修</a-select-option>
					<a-select-option value="报废">报废</a-select-option>
					<a-select-option value="返工">返工</a-select-option>
					<a-select-option value="备料">备料</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="报工模式：" name="typeName">
				<a-radio-group v-model:value="formData.typeName">
					<a-radio value="数量">数量</a-radio>
					<a-radio value="状态">状态</a-radio>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="描述：" name="description">
				<a-input v-model:value="formData.description" placeholder="请输入描述" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="sendLoading">保存</a-button>
		</template>
	</form-container>
	<user-selector-plus ref="UserSelectorPlus" :org-tree-api="selectorApiFunction.orgTreeApi"
		:user-page-api="selectorApiFunction.userPageApi" :checked-user-list-api="selectorApiFunction.checkedUserListApi"
		@onBack="userBack" />
</template>

<script setup name="messageForm">
import { required } from '@/utils/formRules'
import { message } from 'ant-design-vue'
import ProcessOperationApi from '@/api/dev/ProcessOperationApi.js'
import userSelectorPlus from '@/components/Selector/userSelectorPlus.vue'
import tool from '@/utils/tool'
import userCenterApi from '@/api/sys/userCenterApi'
import userApi from '@/api/sys/userApi'

const sendLoading = ref(false)
let UserSelectorPlus = ref()
// 定义emit事件
const emit = defineEmits({ successful: null })
// 默认是关闭状态
let visible = ref(false)
const formRef = ref()
// 表单数据
const formData = ref({})
let userList = ref([])
// 打开抽屉
const onOpen = () => {
	visible.value = true
	formData.value = {}
}
// 关闭抽屉
const onClose = () => {
	emit('successful')
	visible.value = false
}
// 默认要校验的
const formRules = {
	code: [required('请输入编码')],
	name: [required('请输入名称')],
	type: [required('请输入类型')],
	typeName: [required('请输入报工模式')],
	description: [required('请输入描述')]
}
// 站内信分类字典
const categoryOptions = tool.dictList('MESSAGE_CATEGORY')
// 打开人员选择器
const openUserSelector = () => {
	let ids = []
	// 打开之前选判断我们刚刚是否选过
	if (userList.value.length > 0) {
		userList.value.forEach((item) => {
			ids.push(item.id)
		})
	}
	UserSelectorPlus.value.showUserPlusModal(ids)
}
// 人员选择回调
const userBack = (value) => {
	userList.value = value
}
// 删除某用户
const removeUserTag = (index) => {
	userList.value.splice(index, 1)
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value.validate().then(() => {
		sendLoading.value = true
		ProcessOperationApi.processOperationSend(formData.value)
			.then(() => {
				message.success('新增客户成功')
				onClose()
			})
			.finally(() => {
				sendLoading.value = false
			})
	})
}

// 传递设计器需要的API
const selectorApiFunction = {
	orgTreeApi: (param) => {
		return userApi.userOrgTreeSelector(param).then((data) => {
			return Promise.resolve(data)
		})
	},
	userPageApi: (param) => {
		return userApi.userSelector(param).then((data) => {
			return Promise.resolve(data)
		})
	},
	checkedUserListApi: (param) => {
		return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
			return Promise.resolve(data)
		})
	}
}
// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
	onOpen
})
</script>
